package com.zerdoor.service.impl;

import com.zerdoor.dao.TaskDetailDao;
import com.zerdoor.service.TaskDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2014/11/3.
 */
@Service
public class TaskDetailServiceImpl implements TaskDetailService {
    @Autowired
    private TaskDetailDao taskDetailDao;

    @Override
    public Map<String, Object> getTaskDetailByTaskId(Integer taskId) {



        Map<String,Object> data  = taskDetailDao.getTaskDetailByTaskId(taskId);

        if(data != null){


            //如果需求技能ID集不为空，那么就进行翻译处理
            if(data.get("skill") != null && !"".equals(data.get("skill").toString())){

                List skill = taskDetailDao.findSkillNameBySkillIds(data.get("skill").toString());

                data.put("skill",skill.toString().substring(1,skill.toString().length()-1));
            }

        }
        return data;
    }

    @Override
    public List<Map<String, Object>> getBusBidinfo(Integer taskId) {
        return taskDetailDao.getBusBidinfo(taskId);
    }

    @Override
    public List<Map<String, Object>> getProcesses() {
        return taskDetailDao.getProcesses();
    }

    @Override
    public Integer getUserBidinfoByTaskId(Integer taskId, Integer userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("taskId", taskId);
        map.put("userId", userId);
        return taskDetailDao.getUserBidinfoByTaskId(map);
    }

    @Override
    public void updateWinItem(int id,int itemId,int userId) throws Exception {
        //判定该任务是否为当前用户发布的任务
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("itemId",itemId);
        map.put("userId",userId);
        Map<String, Object> result1 = taskDetailDao.getTotalItemByItemIdAndUserId(map);
        if(result1 != null){
            if(Integer.parseInt(result1.get("total").toString()) != 1){
                throw new Exception("无效操作");
            }
        }else{
            throw new Exception("设置中标失败");
        }
        //判定当前任务是否已经有人中标
        Map<String, Object> result2 = taskDetailDao.getWinTotalByItemId(itemId);
        if(result2 != null){
            if(Integer.parseInt(result2.get("total").toString()) != 0){
                throw new Exception("已有人中标");
            }
        }else{
            throw new Exception("设置中标失败");
        }

        try{
            //设置中标
            taskDetailDao.updateWinItem(id);
            //选定艺术家进度改变
            taskDetailDao.updateItemProgress(itemId);
            Map<String, Object> map1 = taskDetailDao.getBaileInfoByUserId(userId);
            map.put("baileId", map1.get("baileId"));
            map.put("bailePhone", map1.get("phone"));
            Map<String, Object> map2 = taskDetailDao.getArtistInfoById(id);
            map.put("artistId", map2.get("artistId"));
            map.put("artistPhone", map2.get("phone"));
            Map<String, Object> map3 = taskDetailDao.getItemInfoByItemId(itemId);
            map.put("title",map3.get("title"));
            Map<String, Object> map4 = taskDetailDao.getBidInfoById(id);
            map.put("totalPrice", map4.get("budget"));
            map.put("pieceNum",0);
            //创建合同模板
            taskDetailDao.addItemContract(map);
        }catch (Exception e){
            throw new Exception("设置中标失败");
        }

    }

    @Override
    public Map<String, Object> getWinTotalByItemId(int itemId) {
        return taskDetailDao.getWinTotalByItemId(itemId);
    }

    @Override
    public void updateTotalNumWinItem(String ids, int itemId, int userId) throws Exception {
        //判定该任务是否为当前用户发布的任务
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("itemId",itemId);
        map.put("userId",userId);
        Map<String, Object> result1 = taskDetailDao.getTotalItemByItemIdAndUserId(map);
        if(result1 != null){
            if(Integer.parseInt(result1.get("total").toString()) != 1){
                throw new Exception("无效操作");
            }
        }else{
            throw new Exception("设置中标失败");
        }
        //判定当前任务是否已经有人中标
        Map<String, Object> result2 = taskDetailDao.getWinTotalByItemId(itemId);
        if(result2 != null){
            if(Integer.parseInt(result2.get("total").toString()) != 0){
                throw new Exception("已有人中标");
            }
        }else{
            throw new Exception("设置中标失败");
        }

        try{
            Map<String, Object> map1 = taskDetailDao.getBaileInfoByUserId(userId);
            map.put("baileId", map1.get("baileId"));
            map.put("bailePhone", map1.get("phone"));
            Map<String, Object> map3 = taskDetailDao.getItemInfoByItemId(itemId);
            map.put("title",map3.get("title"));
            map.put("totalPrice", map3.get("budget"));
            String [] arr = ids.split(",");
            for(int i=0;i<arr.length;i++){
                //设置中标
                taskDetailDao.updateWinItem(Integer.parseInt(arr[i]));
                Map<String, Object> map2 = taskDetailDao.getArtistInfoById(Integer.parseInt(arr[i]));
                map.put("artistId", map2.get("artistId"));
                map.put("artistPhone", map2.get("phone"));
                map.put("pieceNum", map2.get("pieceNum"));
                //创建合同模板
                taskDetailDao.addItemContract(map);
            }
            //选定艺术家进度改变
            taskDetailDao.updateItemProgress(itemId);
        }catch (Exception e){
            throw new Exception("设置中标失败");
        }
    }

}
